package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.bean.User;
import com.ws.ssmzongjie3.bean.UserLikes;
import com.ws.ssmzongjie3.service.RouteService;
import com.ws.ssmzongjie3.service.UserLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dianzan")
public class UserLikesController {
    @Autowired
    private UserLikesService userLikesService;
    @Autowired
    private RouteService routeService;
    //点赞
    @RequestMapping("/qianlist")
    public String qianlist(Model model, ProVo proVo, Route route, HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println("links:"+route);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Route> collections = routeService.listRouteWithLikes(route,user.getUser_id(),true);
        PageInfo pageInfo = new PageInfo(collections);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route",route);

        return "/WEB-INF/qiantai/like/list";
    }
    @PostMapping("/insert")
    @ResponseBody
    public Map<String, Object> insertDianzan(UserLikes userLikes) {
        Map<String, Object> result = new HashMap<>();
        try {

            // 检查用户是否已经点赞
            List<UserLikes> userLikes3 = userLikesService.listUserLike(userLikes);

            userLikes.setLike_time(new java.util.Date().toString()); // 设置当前时间
            if (userLikes3.size() > 0) {
                // 如果已经点赞，取消点赞
                UserLikes userLikes1 = new UserLikes();
                userLikes1.setRoute_id(userLikes.getRoute_id());
                userLikes1.setUser_id(userLikes.getUser_id());
                userLikesService.deleteUserLikeByRouteIdAndUserId(userLikes1);

                result.put("isLiked", false);
            } else {
                // 如果未点赞，添加点赞记录
                userLikesService.insertUserLike(userLikes);
                result.put("isLiked", true);
            }

            // 获取当前路线的点赞总数
            UserLikes userLikes1 = new UserLikes();
            userLikes1.setRoute_id(userLikes.getRoute_id());
            List<UserLikes> userLikes2 = userLikesService.listUserLike(userLikes1);
            result.put("success", true);
            result.put("likeCount", userLikes2.size());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    @RequestMapping("/delete")
    public String delete(UserLikes userLikes) {
        userLikesService.deleteUserLikeByRouteIdAndUserId(userLikes);
        return "redirect:/dianzan/qianlist";
    }

}